package com.rech.xbexpress3;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.rech.utils.CommandUtil;
import com.rech.utils.LogUtil;
import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.support.constraint.Constraints.TAG;
public class LocalLogic {
    //private static String LocalDomain = "http://127.0.0.1:9001";//http://www.xmxbox.com";//http://app.yejianbao.com
    private static String RemoteDomain = "http://www.xmxbox.com";//http://www.xmxbox.com";//http://app.yejianbao.com
    private static LocalLogic singletonLocalLogic = new LocalLogic();
    //将构造器设置为private禁止通过new进行实例化
    private LocalLogic() {

    }
    public static LocalLogic getInstance() {
        return singletonLocalLogic;
    }
    public boolean openGird(JSONObject obj){
        try{
            int gridModel = obj.getJSONObject("data").getInt("gridModel");

            String girdNum = obj.getJSONObject("data").getString("gridNum");
            int IO = obj.getJSONObject("data").getInt("IO");
            int _485 = obj.getJSONObject("data").getInt("485");
            obj.put("source","local");
            obj.getJSONObject("data").put("status","1");
            String cmd = CommandUtil.toGetCommand(IO,_485);
            LogUtil.getInstance().info(OtherUtil.getCurrentTime()+" RESTFUL:"+obj.toString());
            QueueService.send(cmd,obj,IO);
            return true;
        }catch (Exception e){
            return false;
        }

    }
    public void postForOpenGridv2ForJJ(String data){
        try{
            JSONObject jsonObject = new JSONObject(data);
            JSONObject dataObject = jsonObject.getJSONObject("data");
            //dataObject.put("type","LOCAL_JJ_OPEN_GRID");
            jsonObject.put("data",dataObject);
            jsonObject.put("token",OtherUtil.getUniqueToken());
            openGird(jsonObject);
            ;
//            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
//            int cacheSize = 10 * 1024 * 1024;
//            OkHttpClient.Builder builder = new OkHttpClient.Builder()
//                    .connectTimeout(10, TimeUnit.SECONDS)
//                    .writeTimeout(10, TimeUnit.SECONDS)
//                    .readTimeout(10, TimeUnit.SECONDS)
//                    .cache(new Cache(file, cacheSize));
//            OkHttpClient client = builder.build();
//
//            MediaType mediaType = MediaType.parse("application/json");
//            RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
//            Request request = new Request.Builder()
//                    .url(LocalDomain+"/")
//                    .post(body)
//                    .addHeader("Content-Type", "application/json")
//                    .build();
//
//            Response response = client.newCall(request).execute();
//
            Message message = new Message();
            message.what=20101;
            EventBus.getDefault().post(message);
        } catch (Exception e) {
//            Message message = new Message();
//            message.what=20901;
//            EventBus.getDefault().post(message);
        }

    }


    public void postForOpenGridByRed(String data){
        try{
            JSONObject jsonObject = new JSONObject(data);
            JSONObject dataObject = jsonObject.getJSONObject("data");
            //dataObject.put("type","CMD_RED_OPEN_GRID");
            jsonObject.put("data",dataObject);
            jsonObject.put("token",OtherUtil.getUniqueToken());
            openGird(jsonObject);
        } catch (Exception e) {
        }

    }

    public void postForReOpenGridv2ForJJ(String data){
        try{
            JSONObject jsonObject = new JSONObject(data);
            JSONObject dataObject = jsonObject.getJSONObject("data");
            //dataObject.put("type","LOCAL_JJ_REOPEN_GRID");
            jsonObject.put("data",dataObject);
            jsonObject.put("token",OtherUtil.getUniqueToken());

            openGird(jsonObject);
//            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
//            int cacheSize = 10 * 1024 * 1024;
//            OkHttpClient.Builder builder = new OkHttpClient.Builder()
//                    .connectTimeout(10, TimeUnit.SECONDS)
//                    .writeTimeout(10, TimeUnit.SECONDS)
//                    .readTimeout(10, TimeUnit.SECONDS)
//                    .cache(new Cache(file, cacheSize));
//            OkHttpClient client = builder.build();
//
//            MediaType mediaType = MediaType.parse("application/json");
//            RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
//            Request request = new Request.Builder()
//                    .url(LocalDomain+"/")
//                    .post(body)
//                    .addHeader("Content-Type", "application/json")
//                    .build();
//
//            Response response = client.newCall(request).execute();

        } catch (Exception e) {
//            Message message = new Message();
//            message.what=10411;
//            EventBus.getDefault().post(message);
        }

    }
    public void postForOpenGridv2ForKD(String data){
        try{
            JSONObject jsonObject = new JSONObject(data);
            JSONObject msgObject = jsonObject.getJSONObject("data");
            //msgObject.put("type","LOCAL_KD_OPEN_GRID");
            jsonObject.put("data",msgObject);
            jsonObject.put("token",OtherUtil.getUniqueToken());

            openGird(jsonObject);
//            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
//            int cacheSize = 10 * 1024 * 1024;
//            OkHttpClient.Builder builder = new OkHttpClient.Builder()
//                    .connectTimeout(10, TimeUnit.SECONDS)
//                    .writeTimeout(10, TimeUnit.SECONDS)
//                    .readTimeout(10, TimeUnit.SECONDS)
//                    .cache(new Cache(file, cacheSize));
//            OkHttpClient client = builder.build();
//
//            MediaType mediaType = MediaType.parse("application/json");
//            RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
//            Request request = new Request.Builder()
//                    .url(LocalDomain+"/")
//                    .post(body)
//                    .addHeader("Content-Type", "application/json")
//                    .build();
//
//            Response response = client.newCall(request).execute();
//
//            Message message = new Message();
//            message.what=10422;
//            message.obj = response.body().string();
//            EventBus.getDefault().post(message);
        } catch (Exception e) {
//            Message message = new Message();
//            message.what=10423;
//            EventBus.getDefault().post(message);
        }
    }
    public void postForOpenGridv2ForQJ(String data){
        try{
            JSONObject jsonObject = new JSONObject(data);
            JSONObject msgObject = jsonObject.getJSONObject("data");
            //msgObject.put("type","LOCAL_QJ_OPEN_GRID");
            jsonObject.put("data",msgObject);
            jsonObject.put("token",OtherUtil.getUniqueToken());
            openGird(jsonObject);

//            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
//            int cacheSize = 10 * 1024 * 1024;
//            OkHttpClient.Builder builder = new OkHttpClient.Builder()
//                    .connectTimeout(10, TimeUnit.SECONDS)
//                    .writeTimeout(10, TimeUnit.SECONDS)
//                    .readTimeout(10, TimeUnit.SECONDS)
//                    .cache(new Cache(file, cacheSize));
//            OkHttpClient client = builder.build();
//
//            MediaType mediaType = MediaType.parse("application/json");
//            RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
//            Request request = new Request.Builder()
//                    .url(LocalDomain+"/")
//                    .post(body)
//                    .addHeader("Content-Type", "application/json")
//                    .build();
//
//            Response response = client.newCall(request).execute();
//
//            Message message = new Message();
//            message.what=10204;
//            message.obj = data;
//            EventBus.getDefault().post(message);
        } catch (Exception e) {
//            Message message = new Message();
//            message.what=20901;
//            EventBus.getDefault().post(message);
        }

    }

}

class FetchOpenLocalGridTaskv2ForJJ extends AsyncTask<Void, Void, Void> {
    String data;
    public FetchOpenLocalGridTaskv2ForJJ(String argdata) {
        this.data = argdata;
    }

    protected Void doInBackground(Void... params) {
        LocalLogic.getInstance().postForOpenGridv2ForJJ(data);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}

class FetchOpenLocalGridTaskByRed extends AsyncTask<Void, Void, Void> {
    String data;
    public FetchOpenLocalGridTaskByRed(String argdata) {
        this.data = argdata;
    }

    protected Void doInBackground(Void... params) {
        LocalLogic.getInstance().postForOpenGridByRed(data);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}

class FetchReOpenLocalGridTaskv2ForJJ extends AsyncTask<Void, Void, Void> {
    String data;
    public FetchReOpenLocalGridTaskv2ForJJ(String argdata) {
        this.data = argdata;
    }

    protected Void doInBackground(Void... params) {
        LocalLogic.getInstance().postForReOpenGridv2ForJJ(data);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}
class FetchOpenLocalGridTaskv2ForQJ extends AsyncTask<Void, Void, Void> {
    String data;
    public FetchOpenLocalGridTaskv2ForQJ(String argdata) {
        this.data = argdata;
    }

    protected Void doInBackground(Void... params) {
        LocalLogic.getInstance().postForOpenGridv2ForQJ(data);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}
class FetchOpenLocalGridTaskv2ForKD extends AsyncTask<Void, Void, Void> {
    String data;
    public FetchOpenLocalGridTaskv2ForKD(String argdata) {
        this.data = argdata;
    }

    protected Void doInBackground(Void... params) {
        LocalLogic.getInstance().postForOpenGridv2ForKD(data);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}